
angular.module('QualityJudge')

.controller('QualityJudge.HomeCtrl', ['$scope', '$q', '$state', 'QualityJudgeMaster', 'QualityJudgeHttpUtil', 'imanUsbPort', 'imanDialog', 'Help', 'QUALITYJUDGECONSTANTS',
    function ($scope, $q, $state, QualityJudgeMaster, QualityJudgeHttpUtil, imanUsbPort, imanDialog, Help, QUALITYJUDGECONSTANTS) {
        $scope.data = {};

        $scope.data.gridOptions = {
            enableRefresh: true,
            enableCheck: true,
            enableOperate: true,
            operateDefs: [
                { name: 'detail', icon: u9ImHd.getFullPath('img/icon_list.png', 'QualityJudge') },
                { name: 'stream', icon: u9ImHd.getFullPath('img/icon_item.png', 'QualityJudge') }
            ],
            columnDefs: [ 
                { col: 'MOLotNumber', name: '批次' },
                { col: 'ItemMasterCode', name: '料号' },
                { col: 'ItemMasterName', name: '品名' },
                { col: 'Barcode', name: '芯片ID' },
                { col: 'WorkName', name: '作业' },
                { col: 'WorkLocationName', name: '工位' },
                { col: 'UserName', name: '特裁人' }
            ],
            data: []
        };

        $scope.search = function (query) {
            imanDialog.showLoading();
            doSearch(query).then(function (master) {
                QualityJudgeMaster.setActiveMasterCode(query);
                QualityJudgeMaster.setActiveMaster(master);
                $state.go('QualityJudge-detail');
                imanDialog.hideLoading();
            }, function () {
                imanDialog.toastCenter('没有找到对应的物料');
                imanDialog.hideLoading();
            });
        };
        $scope.onOperate = function (operate, item) {
            if (operate === 'detail') {
                QualityJudgeMaster.setActiveMasterCode(item.Barcode);
                $state.go('QualityJudge-detail');
            } else if (operate === 'stream') {
                var productStreamAppID = QUALITYJUDGECONSTANTS.productStreamAppID;
                Help.loadApp(productStreamAppID).then(function () {
                    $state.go(productStreamAppID, {
                        Barcode: item.Barcode
                    });
                }, function () {
                    imanDialog.toastCenter('进入单品流水应用失败，请确认是否已正确安装然后重试。');
                });
            }
        };
        $scope.doRefresh = function (operate, item) {
            init(true).finally(function() {
               $scope.$broadcast('scroll.refreshComplete');
            });
        };
        $scope.doJudge = function (qcStatus) {
            var judgeBatch = [];
            angular.forEach($scope.data.gridOptions.data, function (master) {
                if (master.checked) {
                    judgeBatch.push(QualityJudgeHttpUtil.doJudge(master.Barcode, qcStatus));
                }
            });
            if (judgeBatch.length === 0) {
                imanDialog.toastCenter('请先选择需要进行品质判定的物料');
                return;
            }
            $q.all(judgeBatch).then(function () {
                imanDialog.toastCenter('品质判定成功');
                init(true);
            });
        };

        $scope.$on('$ionicView.afterEnter', function () {
            init(true);
        });

        $scope.$on('$ionicView.beforeEnter', function () {
            imanUsbPort.start();
        });
        $scope.$on('$ionicView.afterLeave', function () {
            imanUsbPort.stop();
        });
        $scope.$on('imanUsbPort.input', function (e, inputStr) {
            imanDialog.showLoading();
            doSearch(inputStr).then(function (master) {
                QualityJudgeMaster.setActiveMasterCode(inputStr);
                QualityJudgeMaster.setActiveMaster(master);
                $state.go('QualityJudge-detail');
                imanDialog.hideLoading();
            }, function () {
                imanDialog.hideLoading();
            });
        });

        function init (forceUpdate) {
            return QualityJudgeMaster.getMasters(forceUpdate).then(function (masters) {
                angular.forEach(masters, function (master) {
                    master.checked = false;
                    if (master.Users.length > 0) {
                        master.UserName = master.Users[0].Name;
                    } else {
                        master.UserName = '';
                    }
                });
                $scope.data.gridOptions.data = masters;
                $scope.data.queryStr = '';
                $scope.data.allChecked = false;
            });
        }

        function doSearch (barCode) {
            var defer = $q.defer();

            QualityJudgeHttpUtil.decideScanCode(barCode).then(function (result) {
                if (result.BarcodeType === 0) {
                    defer.resolve(result);
                } else {
                    defer.reject();
                }
            }, function (err) {
                defer.reject(err);
            });

            return defer.promise;
        }
    }
]);